package com.duobaoyu.dby.supply.admin.controller.system;

import com.duobaoyu.dby.channel.adapter.enums.ChannelCodeEnum;
import com.duobaoyu.dby.common.core.util.R;
import com.duobaoyu.dby.common.security.annotation.Inner;
import com.duobaoyu.dby.supply.biz.client.boundary.auth.ChannelAccessTokenBoundaryClient;
import com.duobaoyu.dby.supply.biz.client.boundary.auth.bean.bo.AuthTokenBO;
import com.duobaoyu.dby.supply.biz.client.boundary.auth.bean.dto.AccessTokenDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName TestTokenController
 * @createTime 09:53:00 2022/05/19
 */
@Inner(value = false)
@RestController
@RequestMapping("/test")
@RequiredArgsConstructor
public class TestTokenController{
    private final ChannelAccessTokenBoundaryClient channelAccessTokenBoundaryClient;

	/**
	 * 初始化渠道Token
	 */
	@GetMapping("/token")
    public R<String> getTokenCode(@RequestParam("channelCode") String channelCode) {
        ChannelCodeEnum channelCodeEnum = ChannelCodeEnum.getChannelCodeEnumByCode(channelCode);
        if(Objects.isNull(channelCodeEnum)){
            return R.failed("渠道编码" + channelCode + "错误!");
        }
		AccessTokenDTO accessTokenDTO = AccessTokenDTO.builder()
				.channelCode(channelCode)
				.build();
		AuthTokenBO authToken = channelAccessTokenBoundaryClient.accessChannelToken(accessTokenDTO);
        return R.ok(authToken.getAccessToken());
    }
}
